#!/bin/bash

check_user=`whoami`
if [ "$check_user" != "dxg" ]
then
    echo "请在dxg用户下执行"
    exit
fi

source ~/.bashrc
nvm use v6.11.2
SRC_BASE=/dxg/src
DIST_BASE=/dxg/dist

function qqNotify(){
    text=$1
    curl -X POST http://codecrop.cc:8000/qq/notify -d "content=$text"
}

prj_folder=$1

if [ "$prj_folder" == "dl" ];then
    ssh root@121.201.69.19 "cd /dxg/dist/dl/ && git pull"
    exit
fi

if [ "$prj_folder" == "" ];then
    PRJS=`find . -maxdepth 1 -name "*-ui"`
    for prj in $PRJS;do
        echo $prj
    done
    read -p 'input project:' prj_folder
fi

#rcNotify $prj_folder

prj_name=`basename $prj_folder`
cd $prj_folder
Branch=`git branch|awk '{print $2}'`
HostName=`hostname`
git stash
git pull
git stash pop


cnpm i

npm run build

rm -rf $DIST_BASE/$prj_name
cp -r dist $DIST_BASE/$prj_name

release_info="$prj_folder is deployed,\n Branch: $Branch ,\n  HostName: $HostName "
echo $release_info
gitinfo=`git log|sed -n 1,6p`
info=`echo -e  "${gitinfo}  \n    $release_info"`
#qqNotify "$info"

git log|sed -n 1,6p

echo "release_ok"
